package edu.tapestry.iddb.client.pages.groupIM;

import iddb.GroupsInfo;
import iddb.PluginIdentificator;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.apache.tapestry5.annotations.ApplicationState;
import org.apache.tapestry5.annotations.InjectPage;
import org.apache.tapestry5.annotations.Property;

import edu.tapestry.iddb.client.entities.GroupIM;
import edu.tapestry.iddb.client.webService.CommandsGenerator;
import edu.tapestry.iddb.client.webService.IDDBProxy;

public class ListGroupIM {

	@ApplicationState
	private IDDBProxy proxy;

	@Property
	private GroupIM currentGroup;
	
	@Property
	private PluginIdentificator ident;

	@InjectPage
	private DetailsGroupIM detailsPage;

	@Property 
	private List<GroupIM> groups;
	
	public Object initialize(PluginIdentificator ident){
		this.ident = ident;
		
		GroupsInfo names = ((GroupsInfo) proxy.execute(CommandsGenerator.getGroupsCmd(ident)));
		

		this.groups = new ArrayList<GroupIM>();

		for (Iterator<String> ii = names.getGroups().iterator(); ii.hasNext();) {
			String name = ii.next();

			groups.add(new GroupIM(name));
		}

		return this;
	}
	
	public List<GroupIM> getGroups() {
		return groups;
	}

	public Object onActionFromDetailsLink(String groupname) {
		return detailsPage.initialize(ident, groupname);

	}

	public GroupIM getCurrentGroup() {
		return currentGroup;
	}

	public void setCurrentBuddy(GroupIM currentGroup) {
		this.currentGroup = currentGroup;
	}

	
}
